export function getCurrentTime(fields = 's',time=null) {
    const date =time? new Date(time):new Date();
    return toTimeStr(date,fields);
}
export function utcToTime(utc,fields = 's') {
    const date = new Date(utc);
    return toTimeStr(date,fields);

}
export function getForm(from,to){
    Object.keys(to).forEach(item=>{
        to[item]=from[item];
    })
}
function toTimeStr(date,fields){
    let str = '';
    let y = date.getFullYear().toString();
    let m = (date.getMonth() + 1).toString();
    m = m.length === 1 ? '0' + m : m
    let d = date.getDate().toString();
    d = d.length === 1 ? '0' + d : d
    let h = date.getHours().toString();
    h = h.length === 1 ? '0' + h : h
    let i = date.getMinutes().toString();
    i = i.length === 1 ? '0' + i : i
    let s = date.getSeconds().toString();
    s = s.length === 1 ? '0' + s : s
    if (fields === 'y') {
        str = y;
    }
    if (fields === 'm') {
        str = y + '-' + m
    }
    if (fields === 'd') {
        str = y + '-' + m + '-' + d
    }
    if (fields === 'h') {
        str = y + '-' + m + '-' + d + ' ' + h
    }
    if (fields === 'i') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i
    }
    if (fields === 's') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
    }
    return str;
}
export function getWeek(time=null){
    const date=time?new Date(time):new Date();
    const weekArr=['星期日','星期一','星期二','星期三','星期四','星期五','星期六']
    return weekArr[date.getDay()];
}
export function getRandomData(max,min,length){
    let arr=[];
    for (let i=0;i<length;i++){
        arr.push((Math.random()*(max-min)+min).toFixed(0))
    }
    return arr;
}
export function getSymbolData(data,symbol){
    let arr = [];
    for (let i = 0; i < data.length; i++) {
        arr.push({
            ...data[i],
            symbolPosition: symbol?symbol:'start',
        })
    }
    return arr;
}
export function staticImageToBase64(image){
    return new Promise((resolve)=>{
        const xhr = new XMLHttpRequest();
        xhr.open('GET', image, true);
        xhr.responseType = 'blob';
        xhr.onload = function() {
            const reader = new FileReader();
            reader.onloadend = function() {
                resolve(reader.result);
            };
            reader.readAsDataURL(xhr.response);
        };
        xhr.send();
    })
}
export function debounce(fn,delay){
    let timer=null;
    return function (...args){
        if(timer) clearTimeout(timer);
        timer=setTimeout(()=>{
            fn.apply(this,args);
            timer=null
        },delay)
    }
}
